<template>
    <div>
    <el-row>
        <el-col :span="2">
            <h2>物品归还</h2>
        </el-col>
        <el-col :span="8" :offset="14" style="text-align: right;">
            <el-button-group v-if="readonly == 'false'">
                <el-button type="primary" @click="passApplication">通过</el-button>
                <el-button type="primary" @click="unpassApplication">驳回</el-button>
            </el-button-group>
            &nbsp;&nbsp;
            <el-button type="primary" @click="reback">取消</el-button>
        </el-col>
    </el-row>

    <el-row>
        <el-col :span="20" :offset="2">
            <el-form ref="form" :model="applicationForm" label-width="80px">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="申请名称"> 
                            <el-input v-model="applicationForm.applicationName" readonly></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请人"> 
                            <el-input v-model="applicationForm.applicationEName" readonly></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="申请部门"> 
                            <el-input v-model="applicationForm.dName" readonly></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="申请时间"> 
                            <el-input v-model="applicationForm.applicationTime" readonly></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="归还仓库"> 
                            <el-select v-model="applicationForm.warehouseId" placeholder="请选择" style="width: 100%" disabled>
                                  <el-option
                                  v-for="item in warehouses"
                                  :key="item.warehouseId"
                                  :label="item.warehouseName"
                                  :value="item.warehouseId">
                                  </el-option>
                              </el-select>
                        </el-form-item>
                    </el-col>
                    
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="备注"> 
                            <el-input type="textarea" v-model="applicationForm.comment" disabled></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-col>
    </el-row>

    <el-row>
        
        <el-col :span="20" :offset="2">
            <el-row>
                <el-col :span="12">
                    <h3 style="text-align: left; margin: 0;">归还列表</h3>
                </el-col>
                <!-- <el-col :span="12" style="text-align: right;">
                    <el-button type="primary" @click="delRGoods">删除</el-button>
                </el-col> -->
            </el-row>
            
            <el-table
                  ref="multipleTable"
                  height="300px"
                  :data="returnGoods"
                  tooltip-effect="dark"
                  style="width: 100%"
                  stripe
                  border
                  highlight-current-row
                  @selection-change="handleSelectionChangeR">
                  <el-table-column
                  type="selection"
                  align="center"
                  width="55">
                  </el-table-column>
                  <el-table-column
                  type="index"
                  label="序号"
                  align="center"
                  width="60">
                  </el-table-column>
                  <el-table-column
                  label="仓库名称"
                  align="center">
                  <template>{{this.warehouses[0].warehouseName}}</template>
                  </el-table-column>
                  <el-table-column
                  prop="goodsName"
                  label="物资名称"
                  align="center">
                  </el-table-column>
                  <el-table-column
                  prop="brNum"
                  label="归还数量"
                  align="center"
                  >
                  
                  </el-table-column>
            </el-table>
        </el-col>
    </el-row>
    <Unpass :isShow="dialogVisable" @closeDialog="closeDialog" @saveBannedReason="saveReason"></Unpass>
  </div>
  </template>
  
  <script>
  import Unpass from '@/components/Unpass.vue';
  export default {
      name: 'AddorEditBApplication',
      components:{
          Unpass
      },
      data(){
          return{
            applicationForm:{
                applicationId:0,
                applicationName:'',
                applicationEmpNo:0,
                applicationEName:'',
                deptno:0,
                dName:'',
                warehouseId:0,
                warehouseName:'',
                applicationTime:'',
                comment:'',
                borrowType:0,
                checkType:0,
            },
            returnGoods:[],
            warehouses:[],
            user:{
                empNo:2,
                role:1,
                eName:"仓管员"
            },
            dialogVisable:false
              
          }
      },
      methods:{
        closeDialog(){
            this.dialogVisable = false
        },
          
        passApplication(){
            let saveData = {}
            this.applicationForm.bannedReason = ""
            this.applicationForm.checkType = 4
            this.applicationForm.auditEmpNo = this.user.empNo
            this.applicationForm.auditEName = this.user.eName
            
            let date = new Date()
            let dateStr = date.getFullYear()
            if (date.getMonth() < 9) {
                dateStr += "-0" + (date.getMonth() + 1)
            } else {
                dateStr += "-" + (date.getMonth() + 1)
            }
            if (date.getDate() < 10) {
                dateStr += "-0" + date.getDate()
            } else {
                dateStr += "-" + date.getDate()
            }
            this.applicationForm.checkTime = dateStr

            saveData.application = this.applicationForm
            saveData.applicationGoods = this.returnGoods
            // console.log(saveData)

            this.$http.post("/lc/updateApplicationStatus",saveData).then(res => { 
                // console.log(res.data)
                
                if (res.data == "success"){
                    this.$router.push({
                        path:"/checkRApplicationManager",
                        query:{
                            msg:"审核完成"
                        }
                    })
                } else{
                    this.$message.error("审核未成功")
                }
            }).catch(err => {
              console.error("Error passing application:", err);
              this.$message.error("审核通过操作失败: " + err.message);
            })
        },
        unpassApplication(){
            this.dialogVisable = true
        },
        reback(){
            this.$router.push({
                path:"/returnApplicationManager"
            })
            
        },
        saveReason(val){
            this.dialogVisable = false
            this.applicationForm.bannedReason = val;

            let saveData = {}
            
            this.applicationForm.checkType = 1
            this.applicationForm.auditEmpNo = this.user.empNo
            this.applicationForm.auditEName = this.user.eName
            
            let date = new Date()
            let dateStr = date.getFullYear()
            if (date.getMonth() < 9) {
                dateStr += "-0" + (date.getMonth() + 1)
            } else {
                dateStr += "-" + (date.getMonth() + 1)
            }
            if (date.getDate() < 10) {
                dateStr += "-0" + date.getDate()
            } else {
                dateStr += "-" + date.getDate()
            }
            this.applicationForm.checkTime = dateStr

            saveData.application = this.applicationForm
            saveData.applicationGoods = this.returnGoods
            console.log(saveData)
            
            this.$http.post("/lc/updateApplicationStatus",saveData).then(res => { 
                // console.log(res.data)
                
                if (res.data == "success"){
                    this.$router.push({
                        path:"/checkRApplicationManager",
                        query:{
                            msg:"审核完成"
                        }
                    })
                } else{
                    this.$message.error("审核未成功")
                }
            }).catch(err => {
              console.error("Error rejecting application:", err);
              this.$message.error("驳回申请失败: " + err.message);
            })
        },
        handleSelectionChangeR(){

        }
  
      },
      mounted(){
        this.readonly = this.$route.query.readonly
        this.applicationForm = this.$route.query.applicationInfo
        // this.returnGoods = this.$route.query.applicationGoods
        this.warehouses = this.$route.query.warehouses
        this.$http.get("/lc/getApplicationGoods?applicationId="+this.$route.query.applicationInfo.applicationId).then(res => {
            // console.log(res.data)
            // console.log(row)
            for (let goods of res.data){
                goods.warehouseName = this.warehouses[this.$route.query.applicationInfo.warehouseId-1].warehouseName
            }
            this.returnGoods = res.data
        }).catch(e => {
            console.log('获取物资列表失败')
        })
        // console.log(this.$route.query.fromPath)
        // this.fromPath = this.$route.query.fromPath
        
      }

  }
  </script>
  
  <style>
  
  </style>